package com.yl.rss;

import org.xmlpull.v1.*;
import android.util.Xml;
import java.io.InputStream;
import android.util.Log;
import com.yl.ifanr.db.RssItemBean;
import com.yl.util.TimeUitl;

public class PullHandler {
	private static final String TAG = "PullHandler";
	private RssFeed rssFeed;
	private RssItemBean rssItem;
	private Boolean startElementFlag = false;
	
	/**
	 * 返回源实例
	 * @return
	 */
	public RssFeed getFeed()
	{
		return rssFeed;
	}
	/**
	 * 解析XML数据
	 */
	public void parseXml(InputStream inStream){
		try{
			XmlPullParser xmlPullParser = Xml.newPullParser();  
			xmlPullParser.setInput(inStream, "UTF-8");  
			int eventType = xmlPullParser.getEventType();  
			boolean isDone = false;
            
			while ((eventType != XmlPullParser.END_DOCUMENT)&&(isDone != true)) {
				String localName = null;
				switch (eventType) { 
					case XmlPullParser.START_DOCUMENT:  
                	{
                		rssFeed = new RssFeed();
                	}  
                	break;  
					case XmlPullParser.START_TAG:  
					{
                    localName = xmlPullParser.getName();  
                    Log.d(TAG, "localName:::"+localName);
                    if(localName.equalsIgnoreCase("item")){
                    		rssItem = new RssItemBean();  
                        startElementFlag = true;
                        Log.d(TAG, "item");
                    }else if(startElementFlag == true){
                        String currentData = null;  
                        if(localName.equalsIgnoreCase("title")) {
                            currentData = xmlPullParser.nextText(); 
                            Log.d(TAG, "title"+currentData);
                            rssItem.setTitle(currentData); 
                        	}else if(localName.equalsIgnoreCase("description")) {  
                            currentData = xmlPullParser.nextText();
                            currentData = this.stripHtml(currentData);
                            Log.d(TAG, "description"+currentData);
                            rssItem.setDescription(currentData);
                        	}else if(localName.equalsIgnoreCase("encoded")){
                            currentData = xmlPullParser.nextText(); 
                            Log.d(TAG, "content:encoded:"+currentData);
                            rssItem.setContent(currentData);
                        	}else if(localName.equalsIgnoreCase("link")){
                        		currentData = xmlPullParser.nextText();
                        		rssItem.setLink(currentData);
                        	}else if(localName.equalsIgnoreCase("pubDate")){
                        		currentData = xmlPullParser.nextText();
                        		rssItem.setPubdate(TimeUitl.getTime(currentData));
                        	}
                    	}                     
					}  
					break;
					case XmlPullParser.END_TAG:  
					{
						localName = xmlPullParser.getName();  
                    	if((localName.equalsIgnoreCase("item"))&&(startElementFlag==true))  
                    	{  
                    		rssFeed.addItem(rssItem);
                    		rssItem = null;
                    		startElementFlag = false;  
                    	}  
					}  
                    break;  
					default:  
                    break;  
				}  
            eventType = xmlPullParser.next();  
			}

		}catch (Exception e){
			e.printStackTrace(); 
		}
	}
	
	/**
	 * 去掉字符串里面的html代码。
	 */
	public String stripHtml(String content) {
		// <p>段落替换为换行
		content = content.replaceAll("<p .*?>", "\r\n");
		// <br><br/>替换为换行 
		content = content.replaceAll("<br\\s*/?>", "\r\n");
		// 去掉其它的<>之间的东西 
		content = content.replaceAll("\\<.*?>", "");
		// 还原HTML 
		// content = HTMLDecoder.decode(content);
		return content; 
	}
}
